package com.andriology.logic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

public class Test implements Parcelable{
	
	private Context context;
	private TestResult resultTestInfo;
	private List<TestQuestion> questionsList;
	private int currentQuestIndex;

	private int numOfQuestions;
	private int testNumber;
	private String testTitleStr;
	
	public Test(Context context, int testNumber, String title){
		this.context = context;
		resultTestInfo  = new TestResult();
		questionsList = new ArrayList<TestQuestion>();
		currentQuestIndex = -1;
		numOfQuestions = -1;
		this.testNumber = testNumber;
		testTitleStr = title;
	}
	
	public TestResult getResultTestInfo() {
		return resultTestInfo;
	}

	public void setQuestions(String[] questionsArray) {
		int scalingFactor = TestQuestion.MAX_NUM_OF_CHOICES + TestQuestion.QUESTION_OFFSET;
		for(int i=0; i<questionsArray.length; i+=scalingFactor)
		{
			TestQuestion questionObject = new TestQuestion();
			questionObject.setQuestion(questionsArray[i]);
			questionObject.setChoices(Arrays.copyOfRange(questionsArray, 
					i+TestQuestion.QUESTION_OFFSET, 
					i+scalingFactor));
			
			int weightsArray [] = new int [TestQuestion.MAX_NUM_OF_CHOICES];
			
			/* load the weights of specific test for the corresponding question number*/
			weightsArray = loadWieghts(testNumber,i/scalingFactor+1);
			
			questionObject.setchoicesWeights(weightsArray);
			questionsList.add(questionObject);	
		}
		
		numOfQuestions = questionsList.size();
	}
	
	public TestQuestion getQuestion(int index){
		if(index < questionsList.size()){
			return questionsList.get(index);
		}
		else
			return null;
		
	}
	
	public TestQuestion getCurrentQuestion(){
		return questionsList.get(currentQuestIndex);
	}
	
	public boolean checkCurrentQuestionIsLast (){
		return (currentQuestIndex == (questionsList.size()-1));
	}
	
	public void resetQuestionsIndex (){
		currentQuestIndex = -1;
	}
	
	public int getNumOfQuestions() {
		return numOfQuestions;
	}
	
	private int[] loadWieghts(int testNumber, int questionNumber){
		/* weights_test_2_question_1*/
		int id =  context.getResources().getIdentifier("weights_test_"+testNumber+"_question_"+questionNumber, "array", context.getPackageName());
		return context.getResources().getIntArray(id);
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeTypedList(questionsList);
		out.writeParcelable(resultTestInfo, flags);
		out.writeInt(currentQuestIndex);
		out.writeInt(numOfQuestions);
		out.writeInt(testNumber);
		out.writeString(testTitleStr);
	}

	/* This static member is called to de-serialize the class object, simply it creates another instance
	 * of the class into which to decode the bytes, that is why we should inialize any 
	 * pointers before using them otherwise Andriod will throw an exception*/
    public static final Parcelable.Creator<Test> CREATOR = 
        new Parcelable.Creator<Test>() {

            @Override
            public Test createFromParcel(Parcel source) {
                return new Test(source);
            }

            @Override
            public Test[] newArray(int size) {
                return new Test[size];
            }
        };
        
        public Test(Parcel in){
    		resultTestInfo  = new TestResult();
    		questionsList = new ArrayList<TestQuestion>();
    		
        	in.readTypedList(questionsList, TestQuestion.CREATOR);
        	resultTestInfo = in.readParcelable(TestResult.class.getClassLoader());
        	currentQuestIndex = in.readInt();
        	numOfQuestions = in.readInt();
        	testNumber = in.readInt();
        	testTitleStr = in.readString();
        }
        

		public int getTestNumber() {
        	return testNumber;
        }
        
        public String getTestTitle() {
        	return testTitleStr;
        }

		public void setCurrentQuestIndex(int currentQuestIndex) {
			this.currentQuestIndex = currentQuestIndex;
		}
		public int getCurrentQuestIndex() {
			return currentQuestIndex;
		}
}
